package com.lpit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lpit.dto.ArticleManagementDto;
import com.lpit.mapper.ArticleMapper;
import com.lpit.mapper.BrowsingHistoryMapper;
import com.lpit.mapper.CommentMapper;
import com.lpit.mapper.CommentReplyMapper;
import com.lpit.pojo.Article;
import com.lpit.pojo.BrowsingHistory;
import com.lpit.pojo.Comment;
import com.lpit.result.ResponseResult;
import com.lpit.service.ArticleManagementService;
import com.lpit.vo.ArticleManagementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
public class ArticleManagementServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleManagementService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentReplyMapper commentReplyMapper;
    @Autowired
    private BrowsingHistoryMapper browsingHistoryMapper;
    /**
     * 查询所有文章
     *
     * @param article
     * @return
     */
    @Override
    public ResponseResult<Page<ArticleManagementVo>> list(ArticleManagementDto article) {
        //对象不能为空
        if (ObjectUtils.isEmpty(article)){
            article = new ArticleManagementDto();
        }
        //page不能为null
        if (ObjectUtils.isEmpty(article.getPage())){
            article.setPage(0);
        }
        //pageSize不能为null,0
        if (ObjectUtils.isEmpty(article.getPageSize()) || article.getPageSize()== 0){
            article.setPageSize(4);
        }
//        IPage<ArticleManagementVo> articlePage = new Page<>(article.getPage(),article.getPageSize());
//        articleMapper.selectPage(articlePage, Wrappers.lambdaQuery(Article.class).eq(!ObjectUtils.isEmpty(article.getUserId()),Article::getUserId,article.getUserId()));
        //将日期类型转换为String类型
        if (!(ObjectUtils.isEmpty(article.getBeginTime()) && ObjectUtils.isEmpty(article.getFinishTime()))){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//此处指定需要转换为那种日期格式
            String begin = dateFormat.format(article.getBeginTime());//开始转换
            String finish = dateFormat.format(article.getFinishTime());
            article.setBeginString(begin);
            article.setFinishString(finish);
        }

//        if(StringUtils.isNotEmpty(article.getTitle())){
//            article.setTitle("%" + article.getTitle() + "%");
//        }
        //创建构造条件
        LambdaQueryWrapper<ArticleManagementVo> wrapper = Wrappers
                .lambdaQuery(ArticleManagementVo.class)
                //根据用户的id进行查询
                .eq(ObjectUtils.isNotEmpty(article.getUserId()),ArticleManagementVo::getUserId,article.getUserId())
                //根据前端提供的文章名称进行模糊查询
                .like(StringUtils.isNotEmpty(article.getTitle()), ArticleManagementVo::getArticleTitle, article.getTitle());

        //调用mapper自定义sql进行分页查询
        Page<ArticleManagementVo> articleManagementVoPage = articleMapper.queryPageByUserId(
                new Page<>(article.getPage(),article.getPageSize()), article,
                wrapper);

        return new ResponseResult<>(200,articleManagementVoPage,"查询成功");
    }

    /**
     * 修改文章
     *
     * @param article
     * @return
     */
    @Override
    public ResponseResult modify(Article article) {

        if (ObjectUtils.isEmpty(article)) {
            return new ResponseResult(504,null,"修改内容为空");
        }

        int i = articleMapper.updateById(article);

        return new ResponseResult(200,i,"修改成功");
    }

    /**
     * 根据id删除文章
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {

//        List<Comment> commentList = commentMapper.selectList(Wrappers.<Comment>lambdaQuery().eq(ObjectUtils.isNotEmpty(id),Comment::getArticleId, id));

        List<Long> longList = commentReplyMapper.selectByArticle(id.longValue());
        //删除回复评论表
        commentReplyMapper.deleteBatchIds(longList);
        //删除评论表
        commentMapper.delete(Wrappers.<Comment>lambdaUpdate().eq(ObjectUtils.isNotEmpty(id),Comment::getArticleId,id));

        //删除文章行为
        browsingHistoryMapper.delete(Wrappers.<BrowsingHistory>lambdaUpdate().eq(ObjectUtils.isNotEmpty(id),BrowsingHistory::getArticleId,id));
        //删除文章
        articleMapper.deleteById(id);

        return new ResponseResult(200,null,"删除成功");
    }

    /**
     * 置顶文章
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult contentTop(Integer id) {

        Article article = articleMapper.selectById(id);
        article.setContentTop(1);
        articleMapper.updateById(article);
        return new ResponseResult(200,null,"置顶成功");
    }

    /**
     * 内容撤回，存放到草稿箱
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult revocation(Integer id) {


        Article article = articleMapper.selectById(id);
        article.setArticleStatus("草稿箱");
        articleMapper.updateById(article);
        return new ResponseResult(200,null,"撤回成功");
    }
}
